package com.fulong.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fulong.entity.*;
import com.fulong.service.*;
import com.fulong.web.webconfig.UserStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fulong.common.utils.PageUtils;
import com.fulong.common.utils.R;



/**
 * 
 *
 * @author fulong
 * @email ${email}
 * @date 2022-03-27 19:36:41
 */
@RestController
@RequestMapping("/devicelendrecord")
public class DeviceLendRecordController {
    @Autowired
    private DeviceLendRecordService deviceLendRecordService;

    @Autowired
    private DeviceManageInfoService deviceManageInfoService;

    @Autowired
    private UserMsgInfoService userMsgInfoService;

    @Autowired
    private DevicePayInfoService devicePayInfoService;

    @Autowired
    private DeviceStatusInfoService deviceStatusInfoService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = deviceLendRecordService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/allpage")
    public R listpage(@RequestBody Map<String, Object> params){
        int currentpage  = 1 ;
        if(params.get("page")!=null) {
            currentpage = Integer.parseInt(params.get("page").toString());
            params.remove("page") ;
        }
        List<DeviceLendRecordEntity> list = deviceLendRecordService.list(new QueryWrapper<DeviceLendRecordEntity>().allEq(params));
        PageUtils page = new PageUtils(list, 10, currentpage);
        return R.ok().put("page", page);
    }

    @RequestMapping("/getbuyrecord")
    public R getbuyRecord(@RequestBody Map<String, Object> params ){

        return  R.ok();
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		DeviceLendRecordEntity deviceLendRecord = deviceLendRecordService.getById(id);

        return R.ok().put("deviceLendRecord", deviceLendRecord);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    public  R save(@RequestBody DeviceLendRecordEntity deviceLendRecord){
        boolean b = false ;
        boolean a = false ;
        if(b=deviceManageInfoService.alterDeviceStatus(2, deviceLendRecord.getDeviceid())) {
            //写入接入信息
            deviceLendRecord.setLenddate(new Date());
            deviceLendRecord.setUserid(UserStatus.getParam().getId());
            deviceLendRecordService.save(deviceLendRecord);
            //修改设备状态
            //发送消息给审批人
            a = userMsgInfoService.createMsgByApprover(deviceLendRecord);
        }
        return a&&b ? R.ok() : R.error();
    }
    /**
     * 修改  采用统一流程 --废弃
     */
    @RequestMapping("/update")
    public R update(@RequestBody DeviceLendRecordEntity deviceLendRecord){
        boolean b = deviceLendRecordService.updateById(deviceLendRecord);
        Integer status = deviceLendRecord.getStatus();
        switch (status) {
            case 3 :
            case 5 :
            case 6 :
            case 8 :
                boolean update1 = deviceManageInfoService.update(new UpdateWrapper<DeviceManageInfoEntity>()
                        .eq("id", deviceLendRecord.getDeviceid())
                        .set("status", 0));
                b = b&&update1;
                break;
            case 11 :
                DevicePayInfoEntity devicePayInfoEntity = devicePayInfoService.getById(deviceLendRecord.getDeviceid());
                int nums = devicePayInfoEntity.getNums();
                List<DeviceManageInfoEntity> list = new LinkedList<>();
                for(int i = 0 ; i < nums ; i++) {
                    DeviceManageInfoEntity d = new DeviceManageInfoEntity();
                    d.setStatus(0);
                    d.setCreattime(devicePayInfoEntity.getTimes());
                    d.setPeriod(devicePayInfoEntity.getPeriod());
                    d.setType(devicePayInfoEntity.getType());
                    d.setLocation(devicePayInfoEntity.getLocation());
                    list.add(d);
                }
                deviceManageInfoService.saveBatch(list);
                devicePayInfoService.update(
                        new UpdateWrapper<DevicePayInfoEntity>().eq("id" ,
                                deviceLendRecord.getDeviceid()).set("state",deviceLendRecord.getStatus()));
                break;
            default:
                break;
        }

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		deviceLendRecordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /*
    *
    * 统一审批流程
    * */
    @RequestMapping("/approverprocess")
    public R recordApproverProcess(@RequestBody DeviceLendRecordEntity deviceLendRecord){
        //保存状态
        // deviceLendRecordService.updateById(deviceLendRecord);

        //查询流程走向
        Integer status = deviceLendRecord.getStatus();
        DeviceStatusInfoEntity process = deviceStatusInfoService.getOne(
                new QueryWrapper<DeviceStatusInfoEntity>()
                        .eq("statuscode", status));
        //获取设备状态流程走向
        Integer devicestatuscode = process.getDevicestatuscode();
        //获取终止号
        Integer endcode = process.getEndcode();
        //流程终止操作 1---流程已经终止，0-->流程流转
        deviceLendRecord.setEnd(endcode);
        //更新流程
        deviceLendRecordService.updateById(deviceLendRecord);

        //更新设备状态 ， 根据流程走向更新，或者保持设备状态不变（-1表示为保持设备上一个状态），否则更新流程走向
        if(devicestatuscode >= 0) {
            boolean update = deviceManageInfoService.update(
                    new UpdateWrapper<DeviceManageInfoEntity>()
                            .eq("id", deviceLendRecord.getDeviceid())
                            .set("status", devicestatuscode));
        }
        return R.ok();
    }



}
